# -*- coding: utf-8 -*
'''
    Created by XiaoInk at 2021/10/10 23:40:50
    Gitee: https://gitee.com/XiaoInk
'''

from flask import request, jsonify

from app import app, dao
from app.macros import macros


@app.before_request
def check_token():
    if not request.path.endswith(('/ok', '/register', '/login')):
        token = request.headers.get('Authorization')
        if not token:
            return jsonify(code=macros.HTTP.StatusBadRequest, msg='必传参数不能为空')

        if not dao.token.get(token):
            return jsonify(code=macros.HTTP.StatusUnauthorized, msg='无效的token')

        # 更新过期时间
        dao.token.expire(token, 60*60)


if __name__ == '__main__':
    pass
